package com.vichat.user.service.impl;

import com.vichat.common.util.NumberUtils;
import com.vichat.common.vo.PageFilter;
import com.vichat.core.dao.BaseDaoI;
import com.vichat.user.entity.SystemMessage;
import com.vichat.user.service.IStaticDataService;
import com.vichat.user.service.ISystemMessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by user on 2016/11/17.
 */
@Service("systemMessageService")
public class SystemMessageServiceImpl implements ISystemMessageService {

    @Autowired
    private BaseDaoI<SystemMessage> systemMessageDao;

    /**
     * 拼装排序字段
     *
     * @param pf
     * @return
     */
    private String orderHql(PageFilter pf) {
        String orderString = "";
        if (pf != null) {
            if ((pf.getSort() != null) && (pf.getOrder() != null)) {
                orderString = " order by " + pf.getSort() + " " + pf.getOrder();
            }
        }
        return orderString;
    }

    /**
     * 获取系统参数数量
     *
     * @param queryBean
     * @return
     * @throws Exception
     */
    public Long countSystemMessage(SystemMessage queryBean) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        String hql = "select count(*) from SystemMessage where state = 1";
        return systemMessageDao.count(hql + systemMessageWhereHql(queryBean, params), params);
    }

    /**
     * 删除系统参数
     *
     * @param msgId
     * @throws Exception
     */
    public void delSystemMessage(long msgId) throws Exception {
        SystemMessage sysStaticDataFDB = systemMessageDao.get(SystemMessage.class, msgId);
        if (sysStaticDataFDB != null) {
            systemMessageDao.delete(sysStaticDataFDB);
        }
    }

    /**
     * 查询系统参数
     *
     * @param queryBean
     * @return
     * @throws Exception
     */
    public List<SystemMessage> querySystemMessage(SystemMessage queryBean, PageFilter pf) throws Exception {
        HashMap params = new HashMap();
        String hql = "from SystemMessage where state = 1";
        long lastMsgId = 0;

        List<SystemMessage> systemMessageList = systemMessageDao.find(hql + systemMessageWhereHql(queryBean, params) + " order by msgId desc ", params, pf.getPage(), pf.getRows());

        if(systemMessageList.size() == 500){
            lastMsgId = systemMessageList.get(systemMessageList.size()).getMsgId();
        }
        if(NumberUtils.isNotNullOrZero(lastMsgId)){
            String sql = "update userdb.system_message set is_read = 3 where uid = :uid and msg_id < :msgId";
            HashMap sqlParams = new HashMap();
            sqlParams.put("uid",queryBean.getUid());
            sqlParams.put("msgId",queryBean.getMsgId());
            systemMessageDao.executeSql(sql,sqlParams);
        }
        return systemMessageList;
    }

    public List<SystemMessage> querySystemMessageByUid(Long uid) throws Exception {
        String hql = "from SystemMessage where state = 1 and uid = :uid order by msgId desc";
        HashMap params = new HashMap();
        params.put("uid", uid);
        return systemMessageDao.find(hql, params);
    }

    /**
     * 根据编号获取系统参数
     *
     * @param msgId
     * @return
     */
    public SystemMessage querySystemMessageByMsgId(long msgId) {
        SystemMessage sysStaticDataFDB = systemMessageDao.get(SystemMessage.class, msgId);
        return sysStaticDataFDB;
    }

    /**
     * 系统参数拼装组装条件
     *
     * @param bean
     * @param params
     * @return
     */
    private String systemMessageWhereHql(SystemMessage bean, Map<String, Object> params) {
        String hql = "";
        if (bean != null) {
            if (StringUtils.isNotBlank(bean.getMsgContent())) {
                hql += " and msgContent like :msgContent";
                params.put("msgContent", "%" + bean.getMsgContent() + "%");
            }
            if (NumberUtils.isNotNullOrZero(bean.getMsgType())) {
                hql += " and msgType = :msgType";
                params.put("msgType", bean.getMsgType());
            }
            if (NumberUtils.isNotNullOrZero(bean.getUid())) {
                hql += " and uid = :uid";
                params.put("uid", bean.getUid());
            }
            if (NumberUtils.isNotNull(bean.getIsReceive())) {
                hql += " and isReceive = :isReceive";
                params.put("isReceive", bean.getIsReceive());
            }
            if (NumberUtils.isNotNull(bean.getIsRead())) {
                hql += " and isRead = :isRead";
                params.put("isRead", bean.getIsRead());
            }
            if (NumberUtils.isNotNullOrZero(bean.getShowType())) {
                hql += " and showType = :showType";
                params.put("showType", bean.getShowType());
            }
            if (NumberUtils.isNotNullOrZero(bean.getObjId())) {
                hql += " and objId = :objId";
                params.put("objId", bean.getObjId());
            }
            if (NumberUtils.isNotNullOrZero(bean.getObjType())) {
                hql += " and objType = :objType";
                params.put("objType", bean.getObjType());
            }
            if (bean.getStartDate() != null) {
                hql += " and msgTime >= :startDate";
                params.put("startDate", bean.getStartDate());
            }
            if (bean.getEndDate() != null) {
                hql += " and msgTime <= :endDate";
                params.put("endDate", bean.getEndDate());
            }
        }
        return hql;
    }

    public SystemMessage saveSystemMessage(SystemMessage bean) {
        if (NumberUtils.isNotNullOrZero(bean.getMsgId())) {
            SystemMessage messageFDB = systemMessageDao.get(SystemMessage.class, bean.getMsgId());
            messageFDB.setIsReceive(bean.getIsReceive());
            messageFDB.setIsRead(bean.getIsRead());
            systemMessageDao.update(messageFDB);
            return messageFDB;
        } else {
            systemMessageDao.save(bean);
            return bean;
        }
    }
}
